package com.factory.test.activity;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ScrollView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.databinding.DataBindingUtil;

import com.factory.test.base.BaseTestActivity;
import com.factory.test.R;
import com.factory.test.databinding.ActivitySerialPortBinding;
import com.yt.serial.SerialPortUtil;

/**
 * 串口测试
 */
public class SerialPortActivity extends BaseTestActivity {
    private ActivitySerialPortBinding binding;
    private SerialPortUtil serialPortUtil;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_serial_port);
        binding.setViewClick(new ViewClick());
        binding.selectcode.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position ==0){
    node = "/dev/ttyS2";
}
if (position == 1){
    node = "/dev/ttyS3";
}
if (position ==2){
    node = "/dev/ttyS5";
}if (position ==3){
    node = "/dev/ttyS6";
}
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    public void appendMessage(String s){
        handler.post(new Runnable() {
            @Override
            public void run() {
                binding.textView.append(s);
                binding.textView.append("\n");
            }
        });

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        }, 100);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHandler!=null){
        mHandler.removeCallbacksAndMessages(null);}
        if(serialPortUtil != null) {
            serialPortUtil.stop();
            serialPortUtil = null;
        }
    }

    String node;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    if(serialPortUtil == null){
                        showWarning("请先打开串口");
                        return;
                    }


                    String s = binding.input.getText().toString();
                    if(isEmpty(s)){
                        showWarning("请输入16进制数据");
                        return;
                    }

                    if(s.length() % 2 != 0){
                        showError("仅支持收发16进制");
                        return;
                    }
                    serialPortUtil.send(s);
                    mHandler.sendEmptyMessageDelayed(1,500);
                    break;
            }
        }


    };


    public class ViewClick{
        public void onOpen(){
//            String node = binding.node.getText().toString();
            String br = binding.baudRate.getText().toString();

            if(isEmpty(node, br)){
                showWarning("请输入节点及波特率");
                return;
            }

            binding.node.setEnabled(false);
            binding.baudRate.setEnabled(false);
            binding.open.setEnabled(false);
            serialPortUtil = new SerialPortUtil(node, Integer.parseInt(br), new SerialPortUtil.UartCallback() {
                @Override
                public void onReceiverData(String s) {
                    if(s != null && !s.equalsIgnoreCase("null")) {
                        appendMessage(s);
                    }
                }
            });
        }

        public void onClose(){
            mHandler.removeCallbacksAndMessages(null);
            if(serialPortUtil != null){
                serialPortUtil.stop();
                serialPortUtil = null;
            }

            binding.node.setEnabled(true);
            binding.baudRate.setEnabled(true);
            binding.open.setEnabled(true);
            binding.send.setEnabled(true);
        }

        public void onSend(){
            Log.d("TAG", "onSend: ");
            binding.send.setEnabled(false);
            mHandler.sendEmptyMessageDelayed(1,500);

        }

        public void onClear(){
            binding.textView.setText("");
        }
    }
}
